package com.appspot.zarm.gomoku.validation;

import com.appspot.zarm.game.PlayerType;
import com.appspot.zarm.gomoku.CellState;
import com.appspot.zarm.gomoku.Coord;
import com.appspot.zarm.gomoku.GomokuBoardFactory;
import com.appspot.zarm.gomoku.GomokuGame;
import com.appspot.zarm.gomoku.transfer.CoordImpl;

public class Evaluator {

	/**
	 * Find out if player have completed rows and win.
	 * @param game
	 * @param type
	 * @return
	 */
	public boolean evaluate(GomokuGame game, PlayerType type) {
		int size = game.getBoard().getSize();
		
		// set target cell state
		CellState targetState = CellState.CROSS;
		if (type == PlayerType.TWO) targetState = CellState.ZERO; 
		
		// horizontal
		for (int i = 0; i < size; i++) {
			int counter = 0;
			for (int j = 0; j < size; j++) {
				Coord position = new CoordImpl(j, i);
				if (game.getBoard().getCell(position).getCellState() == targetState) {
					counter++;
				}
			}
			if (counter == GomokuBoardFactory.WIN_SIZE) {
				return true;
			}
		}
		
		// vertical
		for (int i = 0; i < size; i++) {
			int counter = 0;
			for (int j = 0; j < size; j++) {
				Coord position = new CoordImpl(i, j);
				if (game.getBoard().getCell(position).getCellState() == targetState) {
					counter++;
				}
			}
			if (counter == GomokuBoardFactory.WIN_SIZE) {
				return true;
			}
		}

		
		return false;
	}

}
